//需要参数mi,用于区分不同的项目
define(function(require,exports,module){
	//uuid生成方法
	(function() {
		// Private array of chars to use
		var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

		Math.uuid = function (len, radix) {
			var chars = CHARS, uuid = [], i;
			radix = radix || chars.length;

			if (len) {
				// Compact form
				for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
			} else {
				// rfc4122, version 4 form
				var r;

				// rfc4122 requires these characters
				uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
				uuid[14] = '4';

				// Fill in random data.  At i==19 set the high bits of clock sequence as
				// per rfc4122, sec. 4.1.5
				for (i = 0; i < 36; i++) {
					if (!uuid[i]) {
						r = 0 | Math.random()*16;
						uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
					}
				}
			}

			return uuid.join('');
		};

		// A more performant, but slightly bulkier, RFC4122v4 solution.  We boost performance
		// by minimizing calls to random()
		Math.uuidFast = function() {
			var chars = CHARS, uuid = new Array(36), rnd=0, r;
			for (var i = 0; i < 36; i++) {
				if (i==8 || i==13 ||  i==18 || i==23) {
					uuid[i] = '-';
				} else if (i==14) {
					uuid[i] = '4';
				} else {
					if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
					r = rnd & 0xf;
					rnd = rnd >> 4;
					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
				}
			}
			return uuid.join('');
		};

		// A more compact, but less performant, RFC4122v4 solution:
		Math.uuidCompact = function() {
			return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
				var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
				return v.toString(16);
			});
		};
	})();

	function tongji(){
		this.init.call(this);
	}
	tongji.prototype={
		init:function(){
			var _this=this;
			this.protocol = (("https:" == document.location.protocol) ? tongji_host_https : tongji_host_http);
			this.obj={};
			if(this.getCookie('tongji')){
				var tongjiCookie=JSON.parse(_this.getCookie('tongji'));
				_this.obj.ui=tongjiCookie.uuid;
				_this.obj.nw=0
			}else{
				_this.obj.ui=Math.uuid(32);

				var cookieObj={isNew:true,uuid:_this.obj.ui};
				_this.setCookie("tongji",JSON.stringify(cookieObj));
				_this.obj.nw=1;
			}
			if(typeof window.sessionStorage.tongji=='undefined'){
				_this.obj.url=_this.getUrl();
				_this.obj.at=_this.getTime();
				_this.obj.display=window.screen.width+"x"+window.screen.height;
				_this.obj.rnd=_this.getRnd(10);
				_this.obj.su=document.referrer;
				_this.obj.si=_this.getRnd(16);
				_this.obj.pk="2aff7328899d45ed8b741016b6a03627";//统计唯一标识，按项目划分不同
				//if(typeof mi=='undefined'){_this.obj.mi='ff014a0e5b27c179'}else{_this.obj.mi=mi};
				if(typeof mi=='undefined' || mi== ''){
					if(_this.getCookie("mid")){
						_this.obj.mi=_this.getCookie("mid");
					}else{
						_this.obj.mi=defaultMid;
					}
				}else{
					_this.obj.mi=mi;
					_this.setCookie('mid',mi);
				};
				_this.obj.od=0;
				window.sessionStorage.tongji=JSON.stringify(_this.obj);
			}else{
				var tongjiSession=JSON.parse(window.sessionStorage.tongji);
				_this.obj=tongjiSession;
				var now=_this.getTime();
				_this.obj.st=now-_this.obj.at;
				_this.obj.url=_this.getUrl();
				_this.obj.at=now;
				_this.obj.od=tongjiSession.od+1;
				_this.obj.su=document.referrer;
				_this.obj.nw=0;
				console.log(_this.obj);
				window.sessionStorage.tongji=JSON.stringify(_this.obj);
			}
			//this.addImg(_this.obj);
		},
		getUrl:function(){
			return window.location.href;
		},
		addImg:function(obj){
			var img = new Image();
			var rnd_id = "_img_" + Math.random();
			window[rnd_id] = img; // 全局变量引用
			img.onload = img.onerror = function () {
				window[rnd_id] = null; // 删除全局变量引用
			}
			if(obj.od==0){
				img.src=this.protocol+"/s.png?ds="+obj.display+"&rnd="+obj.rnd+"&ui="+obj.ui+"&si="+obj.si+"&pk="+obj.pk+"&mi="+obj.mi+"&at="+obj.at+"&od="+obj.od+"&nw="+obj.nw+"&su="+obj.su;
			}else{
				img.src=this.protocol+"/s.png?rnd="+obj.rnd+"&ui="+obj.ui+"&si="+obj.si+"&pk="+obj.pk+"&mi="+obj.mi+"&at="+obj.at+"&od="+obj.od+"&su="+obj.su+"&st="+obj.st;
			}
		},
		getTime:function(){//时间
			return Math.round(new Date().getTime()/1000);
		},
		getRnd:function(num){
			var max=Math.pow(10,num);
			var min=Math.pow(10,num-1);
			return parseInt(Math.random()*(max-min+1)+min);
		},
		setCookie:function(name,value){//写cookies
			var Days = 1000;
			var exp = new Date();
			exp.setTime(exp.getTime() + Days*24*60*60*1000);
			document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/";
		},//读取cookies
		getCookie:function(name)
		{
			var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

			if(arr=document.cookie.match(reg))
				return unescape(arr[2]);
			else
				return null;
		},
		//删除cookies
		delCookie:function(name)
		{
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval=getCookie(name);
			if(cval!=null)
				document.cookie= name + "="+cval+";expires="+exp.toGMTString();
		}
	}
	var myTongji=new tongji();
	myTongji.addImg(myTongji.obj);

	module.exports = myTongji;
});